package customizeservicelogic

import (
	"context"

	"property_service/internal/svc"
	"property_service/property_service"

	"github.com/zeromicro/go-zero/core/logx"
)

type SelectBySqlLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSelectBySqlLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SelectBySqlLogic {
	return &SelectBySqlLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// SelectBySql 自定义Sql查询返回字符串
func (l *SelectBySqlLogic) SelectBySql(in *property_service.Sql) (*property_service.ResponseStr, error) {
	//判断字符是否为空
	if in.Sql == "" {
		return &property_service.ResponseStr{
			Message: "sql不能为空",
			Str:     "",
			Code:    404,
		}, nil
	}

	//获取 Gorm 数据库连接
	DB := l.svcCtx.Gorm
	//执行SQL语句，传入参数：sql
	var ss string // 声明一个字符串变量用于存储查询结果
	DB.Raw(in.Sql).Scan(&ss)

	return &property_service.ResponseStr{
		Message: "执行成功",
		Str:     ss,
		Code:    200,
	}, nil
}
